using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace CodePlex.Resourceful.Bookmarks.Model
{
    public class Bookmark
    {
        private string _userID;
        private string _uri;
   
        private string _shortDescription;
        private string _longDescription;
        private DateTime _timestamp;
        private bool _isPublic;

        public bool IsPublic
        {
            get { return _isPublic; }
            set { _isPublic = value; }
        }
	

        public DateTime Timestamp
        {
            get { return _timestamp; }
            set { _timestamp = value; }
        }
	

        public string LongDescription
        {
            get { return _longDescription; }
            set { _longDescription = value; }
        }
	

        public string ShortDescription
        {
            get { return _shortDescription; }
            set { _shortDescription = value; }
        }
	

        public string URIHash
        {
            get
            {
                return Utils.ToBase16(Utils.ToMD5(this.URI, Encoding.UTF8));
            }
        }
	

        public string URI
        {
            get { return _uri; }
            set { _uri = value; }
        }
	

        public string UserID
        {
            get { return _userID; }
            set { _userID = value; }
        }





        public void ToXml(XmlTextWriter xwriter)
        {
           
            using (Utils.StartElement(xwriter,"bookmark"))
            {
                xwriter.WriteElementString("is-public", this.IsPublic.ToString());
                xwriter.WriteElementString("timestamp", this.Timestamp.ToString());
                xwriter.WriteElementString("long-description", this.LongDescription);
                xwriter.WriteElementString("short-description", this.ShortDescription);
                xwriter.WriteElementString("uri", this.URI);
                xwriter.WriteElementString("user-id", this.UserID);
            }

            xwriter.Flush();
        }


        public static Bookmark FromXml(TextReader reader)
        {
            Bookmark rt = new Bookmark();

            XmlTextReader xreader = new XmlTextReader(reader);

            while (xreader.Read())
            {
                if (xreader.Name == "is-public")
                {
                    rt.IsPublic = bool.Parse(xreader.ReadElementContentAsString());
                }
                if (xreader.Name == "timestamp")
                {
                    rt.Timestamp = DateTime.Parse(xreader.ReadElementContentAsString());
                }
                if (xreader.Name == "long-description")
                {
                    rt.LongDescription = xreader.ReadElementContentAsString();
                }
                if (xreader.Name == "short-description")
                {
                    rt.ShortDescription = xreader.ReadElementContentAsString();
                }
                if (xreader.Name == "uri")
                {
                    rt.URI = xreader.ReadElementContentAsString();
                }
                if (xreader.Name == "user-id")
                {
                    rt.UserID = xreader.ReadElementContentAsString();
                }

            }

            return rt;
        }

       

        
    }
}
